<template>
  <div class="reportFormPage">
    <TopOperation ref="TopOperation" :tabs-list="tabList" :active-val.sync="activeVal" @currentActive="currentActive"
      @reportFormsSubmit="reportFormsSubmit" @printSpecificPart="onPrintSpecificPart" />
    <!-- 砂浆拌合物记录表 -->
    <div v-show="activeVal == 'sjbhwxnsyjcjl'" v-for="(item, index) in excelTypeMap['sjbhwxnsyjcjl']"
      :key="'sjbhwxnsyjcjl' + index">
      <sjbhwxnsyjcjl ref="sjbhwxnsyjcjl" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 砂浆拌合物性能检测报告 -->
    <div v-show="activeVal == 'sjbhwxnsyjcbg'" v-for="(item, index) in excelTypeMap['sjbhwxnsyjcbg']"
      :key="'sjbhwxnsyjcbg' + index">
      <sjbhwxnsyjcbg ref="sjbhwxnsyjcbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 砂浆抗压强度记录表 -->
    <div v-show="activeVal === 'sjkyqd'" v-for="(item, index) in excelTypeMap['sjkyqd']" :key="'sjkyqd' + index">
      <sjkyqd ref="sjkyqd" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 砂浆抗压强度报告表 -->
    <div v-show="activeVal === 'sjkyqdbg'" v-for="(item, index) in excelTypeMap['sjkyqdbg']" :key="'sjkyqdbg' + index">
      <sjkyqdbg ref="sjkyqdbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 砂浆凝结时间记录表 -->
    <div v-show="activeVal === 'sjnjsj'" v-for="(item, index) in excelTypeMap['sjnjsj']" :key="'sjnjsj' + index">
      <sjnjsj ref="sjnjsj" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 砂浆配合比记录表 -->
    <div v-show="activeVal === 'sjphbsjsyjcjl'" v-for="(item, index) in excelTypeMap['sjphbsjsyjcjl']"
      :key="'sjphbsjsyjcjl' + index">
      <sjphbsjsyjcjl ref="sjphbsjsyjcjl" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 砂浆配合比报告表 -->
    <div v-show="activeVal === 'sjphbsjsyjcbg'" v-for="(item, index) in excelTypeMap['sjphbsjsyjcbg']"
      :key="'sjphbsjsyjcbg' + index">
      <sjphbsjsyjcbg ref="sjphbsjsyjcbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <backtop-view :sum="activeVal ? excelTypeMap[activeVal].length : 1" @onModPages="onModPages" />
    <statement-select-view ref="statementSelectView" :data-list="tabList" @onShowPreview="onShowPreview" />
    <statement-select-dialog ref="statementSelectDialog" />
  </div>
</template>

<script>
import { getBsDataReport, bsDataReportTb, bsDataReportModPages } from "@/api/syrw/bsDataReport";
export default {
  name: "index",
  components: {
    StatementSelectView: () => import("@/views/reportForms/components/StatementSelectView"),
    StatementSelectDialog: () => import("@/views/reportForms/components/StatementSelectDialog"),
    TopOperation: () => import("@/views/reportForms/components/topOperation"), // tab页
    BacktopView: () => import("@/views/reportForms/components/backtopView"), // 回到顶部页
    sjbhwxnsyjcjl: () => import("@/views/reportForms/sj/sjbhwxnsyjcjl"), //  砂浆拌合物记录
    sjbhwxnsyjcbg: () => import("@/views/reportForms/sj/sjbhwxnsyjcbg"), //  砂浆拌合物报告
    sjkyqd: () => import("@/views/reportForms/sj/sjkyqd"), // 砂浆抗压强度记录
    sjkyqdbg: () => import("@/views/reportForms/sj/sjkyqdbg"), // 砂浆抗压强度报告
    sjnjsj: () => import("@/views/reportForms/sj/sjnjsj"), //  砂浆凝结时间记录
    sjphbsjsyjcjl: () => import("@/views/reportForms/sj/sjphbsjsyjcjl"), // 砂浆配合比记录
    sjphbsjsyjcbg: () => import("@/views/reportForms/sj/sjphbsjsyjcbg") // 砂浆配合比报告
  },
  data () {
    return {
      isPdf: false,
      reportId: undefined,
      tabList: [],
      form: {
        syType: "",
        excelTypeList: {}
      },
      activeVal: "",
      // key为excelTypeId， value为 excelId数组
      excelTypeMap: {}
    };
  },
  watch: {
    'activeVal' (val) {
      this.reportFormsSubmit(false);
    }
  },
  created () {
    if (this.$route.query) {
      this.reportId = this.$route.query.reportId;
    }
    this.init();
  },
  methods: {
    // 选择报表
    onPrintSpecificPart () {
      this.$refs.statementSelectView.init();
    },
    //预览
    onShowPreview (tableList) {
      if (!tableList || tableList.length <= 0) {
        return;
      }
      let arr = [];
      this.form.excelTypeList.forEach((item) => {
        if (tableList.includes(item.label)) {
          arr.push({ label: item.label, excelId: item.excelId, syType: this.form.syType, typeSort: item.excelIndex });
        }
      });
      this.$refs.statementSelectDialog.init(arr);
    },
    // 加一页减一页
    async onModPages (modType) {
      // 1先保存
      await this.reportFormsSubmit();
      // 2 加减页
      await this.doModPages(modType);
      // 3 重新加载
      await this.init();
      // 4 刷新
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            this.$refs[item.val][index].getInfo();
          }
        });
      });
    },
    // 加减页
    async doModPages (modType) {
      const arr = this.excelTypeMap[this.activeVal];
      const data = {
        reportId: this.reportId,
        label: this.activeVal,
        modType: modType,
        excelId: Number(modType) < 0 ? arr[arr.length - 1] : undefined,
        vo: Number(modType) < 0 ? undefined : this.$refs[this.activeVal][arr.length - 1].submitForm()
      };
      await bsDataReportModPages(data);
    },
    // 批量提交
    async reportFormsSubmit (hasTips = true) {
      let arr = [];
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            let info = this.$refs[item.val][index].submitForm();
            arr.push({ label: item.val, excelObj: info });
          }
        });
      });
      await bsDataReportTb(arr);
      if (hasTips) {
        this.$modal.msgSuccess("操作成功");
      };
    },
    // 获取记录表和报告表
    async init () {
      if (this.reportId) {
        getBsDataReport(this.reportId).then((response) => {
          this.form = response.data;

          if (!this.form.excelTypeList || this.form.excelTypeList.length == 0) {
            this.form.excelTypeList.push({
              val: undefined,
              name: undefined
            });
            this.tabList = [];
            this.tabList = this.form.excelTypeList;
          } else {
            this.form.excelTypeList.forEach((item) => {
              if (this.excelTypeMap.hasOwnProperty(item.label)) {
                this.excelTypeMap[item.label].push(item.excelId);
              } else {
                this.excelTypeMap[item.label] = [item.excelId];
                this.tabList.push({
                  val: item.label,
                  name: item.excelTypeName
                });
              }
            });
          }

          this.activeVal = this.tabList[0].val;
        });
      } else {
        this.$message.error("报告id不能为空");
      }
    },
    currentActive (val) {
      this.activeVal = val;
    }
  }
};
</script>

<style scoped></style>
